package com.kaikeba.wx.controller;

import com.kaikeba.bean.Expressman;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.mvc.ResponseView;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QRCodeController {
    @ResponseView(value = "/wx/createExpressQRCode.do")
    public String createExpressQRCode(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        req.getSession().setAttribute("qrcode", "code_"+code);
        return "/wx/personQRcode.html";
    }

    @ResponseView(value = "/wx/createUserQRCode.do")
    public String createUserQRCode(HttpServletRequest req, HttpServletResponse resp){
        User user = UserUtil.getWxUser(req.getSession());
        Expressman man = UserUtil.getWxExpressman(req.getSession());
        if(user != null){
            req.getSession().setAttribute("qrcode", "phone_"+user.getPhone());
        }else if(man != null){
            req.getSession().setAttribute("qrcode", "phone_"+man.getPhone());
        }
        return "/wx/personQRcode.html";
    }

    @ResponseBody(value = "/wx/getQRCode.do")
    public String getQRCode(HttpServletRequest req, HttpServletResponse resp){
        String qrcode = (String) req.getSession().getAttribute("qrcode");
        Message msg = new Message();
        if(qrcode == null || qrcode == ""){
            msg.setStatus(-1);
        }else{
            msg.setResult(qrcode);
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/wx/updataStatus.do")
    public String updateStatus(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        boolean result = ExpressService.updateStatus(code);
        Message msg = new Message();
        if(result){
            msg.setStatus(1);
            msg.setResult("取件成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("取件失败，请用户更新二维码");
        }
        return JSONUtil.toJson(msg);
    }
}
